<?php
 require('main.php');
 
 class Service extends Main{
 	
 	function Service(){
 		
 		parent::Main();
 		$this->database();
 		$this->lib('message');
 		$this->lib("pagination");		
 	}
 	
 	function show(){
 		
 		$this->my_view('manage/service/service',$this->serviceList() );
 	}
 	function delete(){
 		$id_sql = join(",",$_POST['id']);
 		$result = $this->db->delete("service","id in($id_sql)");
 		if($result){
 			$this->message->set("恭喜你,删除服务成功!");
 			$this->my_view("manage/service/service",$this->serviceList() );
 		}
 		else{
 			
 			$this->message->set("抱歉,删除服务失败！");
 			$this->my_view("manage/service/service",$this->serviceList() );
 		}
 		
 	}
 	
 	/*
 	 * 
 	 * 服务的添加
 	 * 
 	 */
 	function init_add(){
 		
 		$this->my_view("manage/service/service_add",$this->languageList() );
 	}
 	function add(){
 		$result = $this->db->insert("service",$this->_fetch_form() );
 		if($result){
 		     $this->message->set("恭喜你,添加服务项成功！");
 		     $this->my_view("manage/service/service_success");
 		}
 		else{
 			$this->message->set("抱歉,添加服务项失败！");
 			$this->my_view("manage/service/service_add",$this->languageList() );
 		}
    }
    
    /*
     *修改
     * 
     */
    function init_update($id){
    	
    	$language_list   = $this->db->find("select * from languages");
    	$service_id_row  = $this->db->find("select * from service where id= $id" );
    	$data = array(
    	'language_list' =>$language_list,
    	'service_id_row'=>$service_id_row
    	);
    	
    	$this->my_view("manage/service/service_update",$data);
    }
    function update(){
    	$language_list   = $this->db->find("select * from languages");
    	$service_id_row  = $this->db->find("select * from service where id= ".$_POST['id'] );
    	$data = array(
    	'language_list' =>$language_list,
    	'service_id_row'=>$service_id_row
    	);
    	
    	
    	$result = $this->db->update("service",$this->_fetch_form(),"id= ".$_POST['id'] );
    	
    	if($result){
    		$this->message->set("恭喜你,修改服务项成功!");
    		$this->my_view("manage/service/service",$this->serviceList() );
    	}
    	else{
    		$this->message->set("抱歉,修改服务项失败！");
    		$this->my_view("manage/service/service_update",$data );
    	}
    	
    }
 	
 	
 	
 	/**
 	 * 查询的服务的列表
 	 * 
 	 */
 	function serviceList(){
 		
 		$total = $this->db->count("service");
 		$config = array(
 		'page_size'=>10,
 		'total_row'=>$total
 		);
 		$this->pagination->init($config);
 		$service_list = $this->db->find("select * from service limit "
 		               .$this->pagination->start_row.",".$this->pagination->page_size );
 		return array(
 		'service_list'=> $service_list
 		);
 		
 	}
 	
 	
 	function languageList(){
 		
 		$language_list = $this->db->find("select * from languages");
 		return array(
 		'language_list'=>$language_list
 		);
 	}
 	
 	function _fetch_form()
 	{
	    return array(
	    'name'			=>$_POST['name'],
	    'title'			=>$_POST['title'],
	    'url_pattern'	=>$_POST['url_pattern'],
	    'language'		=>$_POST['language'],
	    'keywords'		=>$_POST['keywords'],
	    'content'		=>$_POST['content'],
	    'description'	=>$_POST['description']
	    ); 

 	 }
 	

 	 
 	 
 	 
 }
?>